﻿using System;
using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns.Controls.Renderers.Windows
{
    public class GridRenderer : ControlRenderer<GridControl, IGridRenderer>, IGridRenderer
    {
        protected override void OnRenderUiXmlBegin(GridControl control, CodeWriter writer)
        {
            writer.WriteBeginTag("Grid");
            RenderControlAttributes(control, writer);
            writer.WriteEnd();
        }

        private static int CountControlsInColumns(GridControl control)
        {
            int max = 0;
            foreach (GridColumn c in control.Columns)
            {
                if (c.HasControls)
                {
                    max = Math.Max(max, c.Controls.Count);
                }
            }
            return max;
        }

        private static int CountControlsInRows(GridControl control)
        {
            int max = 0;
            foreach (GridRow c in control.Rows)
            {
                if (c.HasControls)
                {
                    max = Math.Max(max, c.Controls.Count);
                }
            }
            return max;
        }

        protected override void OnRenderUiXmlContent(GridControl control, CodeWriter writer)
        {
            writer.WriteFullTag("Grid.ColumnDefinitions");
            if (control.Columns.Count > 0)
            {
                foreach (GridColumn c in control.Columns)
                {
                    c.RenderGridColumnDefinition(writer);
                }
            }
            else
            {
                int max = CountControlsInRows(control);
                for (int i = 0; i < max; i++)
                {
                    writer.WriteFullEndTag("ColumnDefinition");
                }
            }
            writer.WriteEndTag("Grid.ColumnDefinitions");

            writer.WriteFullTag("Grid.RowDefinitions");
           // writer.Write(string.Format("<!--{0}-->", control.Rows.Count));
            if (control.Rows.Count > 0)
            {
                foreach (GridRow c in control.Rows)
                {
                    c.RenderGridRowDefinition(writer);
                }
            }
            else
            {
                int max = CountControlsInColumns(control);
                for (int i = 0; i < max; i++)
                {
                    writer.WriteFullEndTag("RowDefinition");
                }
            }
            writer.WriteEndTag("Grid.RowDefinitions");

            int ci = 0;
            foreach (GridColumn c in control.Columns)
            {
                c.GridColumnNr = ci;
                c.RenderUiXml(writer);
                ci++;
            }

            int ri = 0;
            foreach (GridRow c in control.Rows)
            {
                c.GridRowNr = ri;
                c.RenderUiXml(writer);
                ri++;
            }

            control.Controls.RenderUiXml(writer);
        }

        protected override void OnRenderUiXmlEnd(GridControl control, CodeWriter writer)
        {
            writer.WriteEndTag("Grid");
        }
    }
}